package com.qfedu.ssm.servcie.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.ssm.dao.UserDao;
import com.qfedu.ssm.entity.User;
import com.qfedu.ssm.servcie.UserService;
import com.qfedu.ssm.vo.PageVo;
import com.qfedu.ssm.vo.TableVo;
import com.qfedu.ssm.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User CheckUserLogin(String username, String password) {
        log.info("接收到的参数为：{} {}",username,password);
        User user = userDao.selectUserByNameAndPwd(username, password);
        if (user != null){
            log.info("返回的参数为：{}",user);
            return user;
        }
        log.info("返回的参数为：{}", (Object) null);
        return null;
    }

    @Transactional(rollbackFor = Exception.class) //增删改
    @Override
    public Boolean register(UserVo userVo, MultipartFile source, String realPath) {
        log.info("收到的参数：{} {} {}",userVo,source,realPath);


        User users = userDao.selectUserByEmail(userVo.getEmail());
        if (users == null){
            try {
                String filename = source.getOriginalFilename();
                String unique = UUID.randomUUID().toString();
                String ext = FilenameUtils.getExtension(filename);
                String uniqueFileName = unique+"."+ext;

                File file = new File(realPath);
                if (!file.exists()) {
                    file.mkdirs();
                }

                //"target\\FirstSSM-1.0-SNAPSHOT\\upload" + uniqueFileName
                source.transferTo(new File(realPath+"\\"+uniqueFileName));
                String imgURL = "target/SSM-1.0-SNAPSHOT/upload/" + uniqueFileName;
                log.info("文件的存放地址：{}",imgURL);

                //String PASSWORD, String EMAIL, String REAL_NAME, int STATUS, String imageUrl

                User user = new User(userVo.getPassword(), userVo.getEmail(), userVo.getReal_name(), 1, imgURL);
                log.info("向dao层接口传参：{}",user);
                int i = userDao.insert(user);
                log.info("dao层接口返回值：{}",i);
                if (i == 1){
                    return true;
                }
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }else {
            return false;
        }




    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public TableVo<User> getAllUsers(PageVo pageVo) {
        //使用PageHelper设置分页条
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        List<User> users = userDao.select();
        List<User> userList = new ArrayList<User>();
        for (User user : users){
            userList.add(user);
        }

        PageInfo pageInfo = new PageInfo(userList);

        log.info("pageInfo:{}",pageInfo);
        TableVo<User> tableVo = new TableVo<User>(0, "ok", pageInfo.getTotal(), pageInfo.getList());
        log.info("tableVo:{}",tableVo);

        return tableVo;
    }
}
